# -*- coding:utf-8 -*-
'''
Created on 2011-8-30

@author: tanfeng
'''

import re
import os,sys

import urllib2

from autogo.auto.models import *

#from litb.db.DBConnector import DBConnection

#db = DBConnection(host='172.16.0.64')
#cur = db.cur
def test():
    #regex = r'.*?id="(.*?)" value="(.*?)".*?'
    regex = r'.*?name=".*?" id="(.*?)" value="(.*?)".*?'
    
    txt = '''<div id="fct27_185"><span class="cur" onclick="tree.load('fct27_185','series');">+</span><span><input type="checkbox" name="fct27_185" id="fct27_185" value="北京1" >北京2</span></div><div id="fct27_312"><span class="cur" onclick="tree.load('fct27_312','series');">+</span><span><input type="checkbox" name="fct27_312" id="fct27_312" value="北汽乘用车" >北汽乘用车</span></div>'''
    
    patten = re.compile(regex, re.DOTALL)
    #print patten
    match = patten.match(txt)
    
    #print match
    #print match.group(1)
    #print match.group(2)
    
    for m in re.finditer(patten, txt):
        print m.group(1), m.group(2)


regex = r'.*?name=".*?" id="(.*?)" value="(.*?)".*?'
def get_page_content(url):
    request = urllib2.Request(url)
    sock = urllib2.urlopen(request)
    page = sock.read()
    return page

def parse():
    # 顶级分类
    url = 'http://edit.autohome.com.cn/admin3.5/OpenNewWindow/AutoTree.ashx?id=car0&type=brand&selected='
    # 从最顶级点
    url = 'http://edit.autohome.com.cn/admin3.5/OpenNewWindow/AutoTree.ashx?id=brand63&type=fct'
    # 从第二级点
    url = 'http://edit.autohome.com.cn/admin3.5/OpenNewWindow/AutoTree.ashx?id=fct63_92&type=series'
    # 从第三级点
    url = 'http://edit.autohome.com.cn/admin3.5/OpenNewWindow/AutoTree.ashx?id=series634&type=year'
    # 从第四级点
    url = 'http://edit.autohome.com.cn/admin3.5/OpenNewWindow/AutoTree.ashx?id=year2609&type=spec'
    
    get_brand()

def get_brand():
    url = 'http://edit.autohome.com.cn/admin3.5/OpenNewWindow/AutoTree.ashx?id=car0&type=brand&selected='
    page = get_page_content(url)
    
    patten = re.compile(regex, re.DOTALL)
    for m in re.finditer(patten, page):
        brand_id = m.group(1)
        brand_name = m.group(2)
        print brand_id, brand_name
        
        brand = AGBrand(brand_name=brand_name)
        brand.save()
        get_fct(brand_id, brand.brand_id)
    
def get_fct(brand_id, parent_id):
    url = 'http://edit.autohome.com.cn/admin3.5/OpenNewWindow/AutoTree.ashx?id=%s&type=fct' % brand_id
    page = get_page_content(url)
    
    patten = re.compile(regex, re.DOTALL)
    for m in re.finditer(patten, page):
        print m.group(1), m.group(2)
        fct_id = m.group(1)
        fct_name = m.group(2)
        
        fct = AGFactory(brand_id=parent_id, factory_name=fct_name)
        fct.save()
        get_series(fct_id, fct.factory_id)

def get_series(fct_id, parent_id):
    url = 'http://edit.autohome.com.cn/admin3.5/OpenNewWindow/AutoTree.ashx?id=%s&type=series' % fct_id 
    page = get_page_content(url)
    
    patten = re.compile(regex, re.DOTALL)
    for m in re.finditer(patten, page):
        print '    ',m.group(1), m.group(2)
        series_id = m.group(1)
        series_name = m.group(2)
        
#        series = AGCarSeries(factory_id=parent_id, series_name=series_name, car_year='2009')
#        series.save()
        
        get_year(series_id, parent_id, series_name)

def get_year(series_id, parent_id, series_name):
    url = 'http://edit.autohome.com.cn/admin3.5/OpenNewWindow/AutoTree.ashx?id=%s&type=year' % series_id
    page = get_page_content(url)
    
    patten = re.compile(regex, re.DOTALL)
    for m in re.finditer(patten, page):
        print '        ', m.group(1), m.group(2)
        year_id = m.group(1)
        year_name = m.group(2)
        series = AGCarSeries(factory_id=parent_id, series_name=series_name, car_year=year_name)
        series.save()
        get_spec(year_id, series.series_id)

def get_spec(year_id, parent_id):
    url = 'http://edit.autohome.com.cn/admin3.5/OpenNewWindow/AutoTree.ashx?id=%s&type=spec' % year_id
    page = get_page_content(url)
    
    patten = re.compile(regex, re.DOTALL)
    for m in re.finditer(patten, page):
        print '            ', m.group(1), m.group(2)
        style_id = m.group(1)
        style_name = m.group(2)
        
        spec = AGCarStyle(series_id=parent_id, car_style_name=style_name)
        spec.save()
    

#parse()
#get_fct()
#get_series()
#get_year()
#get_spec()
